package com.hosp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Role;
import com.hosp.bean.RoleExample;
import com.hosp.bean.RoleMenu;
import com.hosp.bean.RoleMenuExample;
import com.hosp.controller.dto.RoleDTO;
import com.hosp.mapper.RoleMapper;
import com.hosp.mapper.RoleMenuMapper;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author ztranscript 2021/9/23 22:12
 **/
@Slf4j
@Service
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 查询列表
     *
     * @param role        查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<Role> list(Role role, PageRequestVO pageRequest) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        String name = role.getrName();
        if (!StringUtils.isEmpty(name)) {
            criteria.andRNameLike("%" + name + "%");
        }
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Role> roles = roleMapper.selectByExample(example);
        return new PageInfo<>(roles);
    }


    /**
     * 保存
     *
     * @param roleDTO 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(RoleDTO roleDTO) {
        Integer rid = roleDTO.getrId();
        List<Integer> mIdList = roleDTO.getMIdList();
        Role role = new Role();
        if (Objects.isNull(rid)) {
            // 新增
            BeanUtils.copyProperties(roleDTO, role);
            roleMapper.insertSelective(role);
            log.debug("@role is {}.", role);

            mIdList.forEach(mId -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setmId(mId);
                roleMenu.setrId(role.getrId());
                log.debug("@roleMenu is {}.", roleMenu);

                roleMenuMapper.insertSelective(roleMenu);
            });
        } else {
            // 修改
            BeanUtils.copyProperties(roleDTO, role);
            RoleExample roleExample = new RoleExample();
            RoleExample.Criteria criteria = roleExample.createCriteria();
            criteria.andRIdEqualTo(role.getrId());
            roleMapper.updateByExampleSelective(role, roleExample);
            log.debug("#role is {}.", role);

            RoleMenuExample example = new RoleMenuExample();
            RoleMenuExample.Criteria exampleCriteria = example.createCriteria();
            exampleCriteria.andRIdEqualTo(rid);

            roleMenuMapper.deleteByExample(example);

            mIdList.forEach(mId -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setmId(mId);
                roleMenu.setrId(role.getrId());
                log.debug("#roleMenu is {}.", roleMenu);

                roleMenuMapper.insertSelective(roleMenu);
            });
        }
        return true;
    }

    /**
     * 查询列表
     *
     * @param rId 查询条件
     * @return 响应
     */
    public RoleDTO selectById(Integer rId) {
        RoleDTO roleDTO = new RoleDTO();
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andRIdEqualTo(rId);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(roles)) {
            BeanUtils.copyProperties(roles.get(0), roleDTO);
        }

        RoleMenuExample roleMenuExample = new RoleMenuExample();
        RoleMenuExample.Criteria criteria1 = roleMenuExample.createCriteria();
        criteria1.andRIdEqualTo(rId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(roleMenuExample);
        if (!CollectionUtils.isEmpty(roleMenus)) {
            List<Integer> mIdList = new ArrayList<>();
            roleMenus.forEach(roleMenu -> {
                mIdList.add(roleMenu.getmId());
            });
            roleDTO.setMIdList(mIdList);
        }

        return roleDTO;
    }

    /**
     * 删除
     *
     * @param rIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> rIds) {
        int sum = 0;
        for (Integer rId : rIds) {
            // role
            RoleExample roleExample = new RoleExample();
            RoleExample.Criteria criteria = roleExample.createCriteria().andRIdEqualTo(rId);
            int row = roleMapper.deleteByExample(roleExample);
            sum += row;
            // role-menu
            RoleMenuExample roleMenuExample = new RoleMenuExample();
            RoleMenuExample.Criteria criteria1 = roleMenuExample.createCriteria().andRIdEqualTo(rId);
            roleMenuMapper.deleteByExample(roleMenuExample);
        }
        return sum == rIds.size();
    }
}
